DELETE my-index-000001

// 基本概况
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "integers": {
          "match_mapping_type": "long",
          "mapping": {
            "type": "integer"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ]
  }
}


// 例子: 实际中不会只单独的匹配类型, 可能是匹配字段名, 这里用到 `match` 和 `unmatch`
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "string_as_long": {
          "match_mapping_type": "string",
          "match":   "long_*", // 比如long开头的字符串映射为long
          "unmatch": "*_text",
          "mapping": {
            "type": "long"
          }
        }
      },
      {
        "is_as_bool": {
          "match_mapping_type": "string",
          "match":   "is*", // 比如 is 开头的字符串映射为boolean
          "mapping": {
            "type": "boolean"
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "long_num": "5", // 这个将会被匹配为 long
  "long_text": "foo", // 这个将会走默认, text, 并且有keyword子类型
  "isOk": "true" // 这个将会被匹配为 bool
}

GET my-index-000001

DELETE my-index-000001

// 例子: 或者是匹配路径
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": { // template name
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}

PUT my-index-000001/_doc/1
{
  "name": {
    "first":  "John",
    "middle": "Winston",
    "last":   "Lennon"
  }
}

GET my-index-000001

DELETE my-index-000001

// 实用的例子
PUT my-index-000001
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings_as_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "strings_as_text": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text"
          }
        }
      },
      {
        "strings_as_text_ext_keywords": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      },
      {
        "strings_as_ip": { // 名称为 geoip 的匹配为ip地址类型
          "match_mapping_type": "string",
          "match": "geoip",
          "mapping": {
            "type": "ip"
          }
        }
      },
      {
        "as_location": { // 名称为 location 的匹配为geo_point
          "match": "location",
          "mapping": {
            "type": "geo_point"
          }
        }
      }
    ]
  }
}

